<template>
    <div>
        <div style="padding: 1px 0">
          <el-input style="width: 200px; font-size:medium" prefix-icon="el-icon-search" placeholder="请输入菜单名" v-model="name"></el-input>
          <el-button class="ml-5" type="primary" @click="load" style="font-size:medium">搜索</el-button>
          <el-button class="ml-5" type="warning" @click="reset" style="font-size:medium">重置</el-button>
        </div>

        <div style="padding: 5px 0">
          <el-button class="el-icon-circle-plus-outline" type="primary" @click="handleAdd2" style="font-size:medium">添加</el-button>
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要批量删除吗?"
                @confirm="handleDeleteBatch">
            <el-button type="danger" slot="reference" style="font-size:medium">批量删除</el-button>
          </el-popconfirm>
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg8'" row-key="id" default-expand-all @selection-change="handleSelectionChange" style="font-size:large">
          <el-table-column type="selection" width="40"></el-table-column>
          <el-table-column prop="id" label="ID" align="center"></el-table-column>
          <el-table-column prop="name" label="名称" align="center"></el-table-column>
          <el-table-column prop="path" label="路径" align="center"></el-table-column>
          <el-table-column label="图标" align="center">
            <template slot-scope="scope">
              <i :class="scope.row.icon" style="font-size: 20px"></i>
            </template>
          </el-table-column>
          <el-table-column prop="description" label="描述" align="center"></el-table-column>
          <el-table-column label="操作" width="343" align="center">
            <template slot-scope="scope">
              <el-button type="primary" @click="handleAdd(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单 <i class="el-icon-plus"></i></el-button>
              <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
                <el-popconfirm
                  class="ml-5"
                  confirm-button-text='确定删除'
                  cancel-button-text='我再想想'
                  icon="el-icon-info"
                  icon-color="red"
                  title="您确定要删除吗?"
                  @confirm="handleDel(scope.row.id)">
                  <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-dialog title="菜单信息" :visible.sync="dialogFormVisible" width="40%">
          <el-form label-width="80px" size="small">
            <el-form-item label="名称">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="路径">
              <el-input v-model="form.path" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="图标">
                <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%">
                  <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                    <i :class="item.value" /> {{ item.name }}
                  </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="描述">
              <el-input v-model="form.description" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'Menu',
  data(){
    return{
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 7,
        name: "",
        dialogFormVisible: false,
        multipleSelection: [],
        form:{},
        isEdit: false,
        options: []
    }
  },
  created(){
    this.load();
  },
  methods:{
    load() {
      this.request.get("/menu", {
          params: {
            name: this.name
          },
        }).then((res) => {
          this.tableData = res.data;
        });
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    reset() {
      this.name = "",
      this.load()
    },
    handleAdd(pid){
      this.dialogFormVisible = true
      this.getIcons();
      this.form = {}
      if(pid){
        this.form.pid = pid
      }
    },
    handleAdd2(){
      this.dialogFormVisible = true
      this.getIcons();
      this.form = {}
    },
    getIcons(){
      this.request.get("/menu/icons").then((res) => {
          this.options = res.data;
        });
    },
    handleEdit(row){
      console.log('row',row);
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
      this.getIcons();
      this.isEdit = true
    },
    handleDel(id){
      console.log('id',id);
      this.request.delete("/menu/delete/"+id).then(res =>{
        if(res.code === '200'){
          this.$message.success('删除菜单成功!')
          this.load()
        }else{
          this.$message.error('删除菜单失败!')
          this.load()
        }
      })
    },
    handleDeleteBatch(){
      let ids = this.multipleSelection.map(v => v.id)
      console.log('ids',ids)
      if(ids.length === 0){
        this.$message.warning('批量删除菜单前,请先勾选若干菜单')
      }else{
        this.request.delete("/menu/deleteBatch/",{data:ids}).then(res =>{
          if(res.code === '200'){
            this.$message.success('批量删除菜单成功!')
            this.load()
          }else{
            this.$message.error('批量删除菜单失败!')
            this.load()
          }
        })
      }
    },
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    save(){
      if(this.isEdit === false){
        console.log('add');
        this.request.post("/menu/add",this.form).then(res =>{
        console.log(res)
        if(res.code === '200'){
          console.log('西瓜');
          this.$notify.success('添加菜单成功!')
          this.dialogFormVisible = false
          this.load()
        }else{
          this.$notify.error('添加菜单失败!')
          this.dialogFormVisible = false
          this.load()
        }
      })
      }else{
        console.log('edit');
        this.request.put("/menu/edit",this.form).then((res) =>{
          if(res.code === '200'){
            this.$message.success("修改菜单信息成功!")
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }else{
            this.$message.error('修改菜单信息失败!')
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }
        })
      }
    },
    cancel(){
      this.dialogFormVisible = false
      this.load()
    },
  }
};
</script>

<style>
.headerBg8 {
  background-color: rgb(201, 158, 147) !important;
}
</style>